﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SVNUG.Presentations.NoSql.Business;

namespace SVNUG.Presentations.NoSql.Application.Forms
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void buttonInitialize_Click(object sender, EventArgs e)
        {
            try
            {
                ExampleInitializer yo = new ExampleInitializer();

                yo.Initialize();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void buttonFindAllCustomers_Click(object sender, EventArgs e)
        {
            try
            {
                var customers = RavenDbContext.Instance.FindAllCustomers();

                this.dataGridViewMain.DataSource = customers;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void buttonFindAllOrders_Click(object sender, EventArgs e)
        {
            try
            {
                var orders = RavenDbContext.Instance.FindAllOrders();

                this.dataGridViewMain.DataSource = orders;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void buttonOrderItems_Click(object sender, EventArgs e)
        {
            try
            {
                var orders = RavenDbContext.Instance.FindAllOrders();

                this.dataGridViewMain.DataSource = (from a in orders
                                                    from b in a.ItemList
                                                    select b).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void buttonUnshipped_Click(object sender, EventArgs e)
        {
            try
            {
                var orders = RavenDbContext.Instance.FindAllUnshippedOrders();

                this.dataGridViewMain.DataSource = orders;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dataGridViewMain_DoubleClick(object sender, EventArgs e)
        {
           
        }

        private void dataGridViewMain_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow viewRow = this.dataGridViewMain.Rows[e.RowIndex];

            if (this.dataGridViewMain.DataSource.GetType() == typeof(List<Customer>))
            {
                Customer cust = viewRow.DataBoundItem as Customer;

                this.dataGridViewMain.DataSource = RavenDbContext.Instance.TryFindOrdersByCustomerId(cust.CustomerId);
                return;
            }
            if (this.dataGridViewMain.DataSource.GetType() == typeof(List<Order>))
            {
                Order cust = viewRow.DataBoundItem as Order;

                this.dataGridViewMain.DataSource = cust.ItemList;
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            RavenDbContext.Instance.StoreCustomer(new Customer());
        }
    }
}
